package com.travel.controller;

import com.travel.common.CommonRes.RestResponse;
import com.travel.common.util.JwtUtil;
import com.travel.common.util.MD5Util;
import com.travel.common.util.PhoneUtil;
import com.travel.pojo.User;
import com.travel.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/Login")
@Api(value = "登录注册接口")
public class LoginController {

    @Resource
    private UserService userService;

    @CrossOrigin
    @ApiOperation(value = "登录接口", notes = "登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "携带参数:手机号(phoneNumber),密码(password)", required = true, dataType = "String", paramType = "body"),
    })
    @PostMapping("/login")
    public RestResponse<String> login(@RequestBody Map<String, Object> account) {
        //获取前端输入的账号和密码
        String phoneNumber = (String) account.get("phoneNumber");
        String password = (String) account.get("password");

        //密码md5加密后与数据库对比
        String code = MD5Util.getMd5(password);
        User userInfo = userService.checkLogin(phoneNumber, code);

        if (userInfo == null) {
            return RestResponse.validfail("手机号或密码错误");
        } else {
            //new一个User对象，填入关键信息传到jwt工具类生成token
            User user = new User();
            user.setUserid(userInfo.getUserid());
            user.setPhonenumber(phoneNumber);
            user.setPassword(password);
            String token = JwtUtil.generateToken(user);

            System.out.println(token);//验证

            return RestResponse.success(200, "登录成功", token);
        }
    }

    @CrossOrigin
    @ApiOperation(value = "注册接口", notes = "注册接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "captcha", value = "验证码", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/register")
    public RestResponse<String> register(@RequestBody Map<String, Object> account) {
        //获取前端输入的手机号
        String phoneNumber = (String) account.get("phoneNumber");
        String captcha = (String) account.get("repass");

        //判断手机号格式是否合法
        if (!PhoneUtil.isMatches(phoneNumber)) {
            return RestResponse.validfail("手机号格式有误");
        }

//        查询数据库中该手机号是否已被注册
        boolean isExist = userService.checkPhone(phoneNumber);
        if (isExist) {
            return RestResponse.validfail("手机号已被注册");
        } else {
            //检测验证码
            if (userService.verify(phoneNumber, captcha)) {
                User user = userService.regist(phoneNumber, captcha);

                User tokenOfUser = new User();
                tokenOfUser.setUserid(user.getUserid());
                tokenOfUser.setPhonenumber(user.getPhonenumber());
                tokenOfUser.setPassword(user.getPassword());
                String token = JwtUtil.generateToken(tokenOfUser);
                return RestResponse.success(200, "欢迎您使用携程", token);
            }
            return RestResponse.validfail("手机号已被注册");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneNumber", value = "手机号", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping("/getCaptcha")
    public RestResponse<String> getCaptcha(String phoneNumber) {
        //判断手机号格式是否合法
        if (!PhoneUtil.isMatches(phoneNumber)) {
            return RestResponse.validfail("手机号格式有误");
        }

        String verificationKey = userService.getCaptcha(phoneNumber);
        System.out.println(verificationKey);

        return RestResponse.success("验证码已发送！");
    }
}